package com.kldy8.kldyw.find.script.scriptshow;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kldy8.kldyw.MyWebActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.find.script.bean.Levy;
import com.kldy8.kldyw.system.UrlBuilder;
import com.kldy8.kldyw.web.AppWebUrl;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by Administrator on 2016/8/31.
 */
public class ScriptSolicitationAdapter extends BaseAdapter {
    private List<Levy> listItems;
    private LayoutInflater layoutInflater;
    ImageLoader loader;

    private Context mContext;

    // Constructor
    public ScriptSolicitationAdapter(Context c) {
        mContext = c;
        layoutInflater = LayoutInflater.from(mContext);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
                .build();
        loader = ImageLoader.getInstance();
        loader.init(config);
    }

    public ScriptSolicitationAdapter(Context c, List<Levy> scripts) {
        mContext = c;
        listItems = scripts;
        layoutInflater = LayoutInflater.from(mContext);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
                .build();
        loader = ImageLoader.getInstance();
        loader.init(config);
    }

    //记录商品选中状态
    public final class ListItemView
    {                //自定义控件集合
        public ImageView image;
        public TextView name;
        public TextView supportingText;
        public TextView price;
        public TextView category;
    }

    public void replaceOrAdd(boolean isInit, List<Levy> listItems) {
        if (isInit) {
            this.listItems = listItems;
        } else {
            if (this.listItems != null) {
                if (listItems != null) {
                    this.listItems.addAll(listItems);
                }
            } else {
                this.listItems = listItems;
            }
        }
    }

    public int getCount() {
        // TODO Auto-generated method stub
        if (null == listItems) {
            return 1;
        } else {
            return listItems.size();
        }
    }

    @Override
    public Object getItem(int i) {
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        ListItemView listItemView = null;
        if (null == listItems || listItems.size() == 0) {
            convertView = layoutInflater.inflate(R.layout.view_notfount, null);
            return convertView;
        }
        else
        {
            if (layoutInflater != null) {
                listItemView = new ListItemView();
                view = layoutInflater.inflate(R.layout.script_solicitation_item, null);
                listItemView.image = (ImageView) view.findViewById(R.id.image);
                listItemView.name = (TextView) view.findViewById(R.id.title);
                listItemView.supportingText = (TextView) view.findViewById(R.id.supportingText);
                listItemView.price = (TextView) view.findViewById(R.id.price);
                listItemView.category = (TextView) view.findViewById(R.id.category);

                final Levy script = listItems.get(position);
                String headUrl = "" + script.image;

                loader.displayImage(headUrl, listItemView.image);
                listItemView.name.setText(script.name);
                listItemView.price.setText(script.price);
                listItemView.category.setText(script.category);
                //item点击事件
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        WebViewActivity.startActivity(mContext, script.url);
                        Intent intent = new Intent();
                        String resultUrl = AppWebUrl.createUrl(script.url);
                        intent.putExtra("url",resultUrl);
                        intent.setClass(mContext, MyWebActivity.class);
                        ((Activity)mContext).startActivity(intent);
                        ((Activity)mContext).overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                    }
                });
            }


            return view;
        }
    }

}
